package com.hrt.manage.product.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.GiftDetail;
import com.hrt.bean.GiftInfor;
import com.hrt.manage.product.dao.GiftDetailDao;

@Repository
public class GiftDetailDaoImpl extends BaseDaoImpl<GiftDetail> implements GiftDetailDao {
	/**分页查询的重写*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<GiftDetail> queryPageByCondition(List conditions,
			int pageIndex, int pageSize) {
		String hql = "select detail from GiftDetail detail";
		Map delcon = new HashMap();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		conditions.add(delcon);
		for (int i = 0; i < conditions.size(); i++) {
			Map con = (Map) conditions.get(i);
			String field = con.get("field").toString();
			String val = con.get("value").toString();
			if (i == 0) {
				hql = hql + " where detail." + field + "=" + val;
			} else {
				hql = hql + " and detail." + field + "=" + val;
			}
		}
		List<GiftDetail> results = queryByPage(hql, pageIndex, pageSize);
		return results;
	}
	/**上下架的查询*/
	@SuppressWarnings("unchecked")
	@Override
	public List<GiftDetail> findIspush(GiftInfor giftInfor) {
		return this.getHibernateTemplate().find("from GiftDetail gd where gd.isdelete=0 and gd.giftInfor.id="+giftInfor.getId());
	}
	
	/**加载礼品规格数据**/
	@SuppressWarnings("unchecked")
	@Override
	public List<GiftDetail> queryGifts(String itemCondition) {
		String hql = "select detail from GiftDetail detail where detail.isdelete=0 and (detail.giftCode like '%"
				+ itemCondition
				+ "%'"
				+ " or detail.proName like '%"
				+ itemCondition + "%'" + ")";

		List<GiftDetail> results = new ArrayList<GiftDetail>();

		results = getHibernateTemplate().find(hql);
		return results;
	}
	@Override
	public GiftDetail getDetailByGiftIdAndRuleId(final Long giftId, final Long ruleId) {
		GiftDetail detail=	getHibernateTemplate().execute(new HibernateCallback<GiftDetail>(){
			@Override
			public GiftDetail doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hql="select detail from GiftDetail detail where detail.id="+ruleId+" and detail.isdelete=0 and detail.giftId="+giftId;
				Query query=session.createQuery(hql);
				
				return (GiftDetail) query.uniqueResult();
			}
			
		});
		return detail;
	}
	@Override
	public Long getGiftTotals(Long giftDetailId) {
		String hql = "select gd from GiftDetail gd where gd.id='"+giftDetailId+"'";
		GiftDetail giftDetail = (GiftDetail) getHibernateTemplate().find(hql).get(0);
		return giftDetail.getTotals();
	}
	
	/** 按是否删除查询 */
	@SuppressWarnings("unchecked")
	@Override
	public List<GiftDetail> findDelAll() {
		return getHibernateTemplate().find("FROM GiftDetail WHERE isDelete=0");
	}
	/**根据礼品基本信息的id找到礼品规格的id集合*/
	@SuppressWarnings("unchecked")
	@Override
	public List<GiftDetail> getDetailIdforId(Long id) {
		return this.getHibernateTemplate().find("FROM GiftDetail detail WHERE isdelete=0 and detail.giftInfor.id="+id);
	}
	
}
